package com.jic.member.impl;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.common.utils.IDUtils;
import com.jic.member.exception.AppRuntimeException;
import com.jic.enums.RedisKey;
import com.jic.member.entity.DisCashLog;
import com.jic.member.entity.MememberUser;
import com.jic.member.mapper.DisCashLogMapper;
import com.jic.member.mapper.MememberUserMapper;
import com.jic.member.request.ApplySaveRequest;
import com.jic.member.request.DisCashLogRequest;
import com.jic.member.request.DisCashLogSaveRequest;
import com.jic.member.service.DisCashLogService;
import com.jic.member.service.MememberUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 提现申请,充值申请表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-09-15 02:05:00
 */
@Service
@Slf4j
public class DisCashLogServiceImpl implements DisCashLogService {

    @Autowired
    private DisCashLogMapper disCashLogMapper;
    @Autowired
    private MememberUserMapper memberUserMap;

    @Autowired
    private RedisClientTemplate redisClientTemplate;

    @Autowired
    private MememberUserService mememberUserService;

    @Override
    public long insert(DisCashLog disCashLog) {
        long flag = disCashLogMapper.insert(disCashLog);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return disCashLogMapper.delete(id);
    }

    @Override
    public long update(DisCashLog disCashLog) {
        return disCashLogMapper.update(disCashLog);
    }

    @Override
    public DisCashLog queryByPrimaryKey(Long id) {
        return disCashLogMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<DisCashLog> query(DisCashLog disCashLog) {
        return disCashLogMapper.query(disCashLog);
    }

    @Override
    public PageResult<DisCashLog> queryPage(DisCashLog disCashLog, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<DisCashLog> list = disCashLogMapper.queryPage(startIndex, pageQuery.getLimit(), disCashLog);
        long count = disCashLogMapper.queryPageCount(disCashLog);
        list.forEach(item -> {
            if(null !=item.getMemberId()){
                final MememberUser mememberUser = memberUserMap.queryByPrimaryKey(item.getMemberId());
                if (null != mememberUser) {
                    item.setMemberName(mememberUser.getNickName());
                }
            }

        });
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }


    @Override
    public BigDecimal getWithdrawalCount(DisCashLog disCashLog) {
        return disCashLogMapper.getWithdrawalCount(disCashLog);
    }

    @Override
    public RestResult reviewStatusByDisCashLog(DisCashLogRequest form) {
        try {
            DisCashLog disCashLog = new DisCashLog();
            BeanUtils.copyProperties(form, disCashLog);
            disCashLogMapper.update(disCashLog);
        }catch (Exception e){
            log.error("error",e);
        }
        return RestResult.success("ok");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RestResult saveDisCashLog(DisCashLogSaveRequest request) {
        log.info("开始进入提现保存==========》" + request.toString());
        try {
            String memStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + request.getToken());
            MememberUser member = JSONArray.parseObject(memStr, MememberUser.class);
            if (member == null) {
                return RestResult.error("4000", "登录失效");
            }
            //插入提现信息
            DisCashLog disCashLog = new DisCashLog();
            BeanUtils.copyProperties(request,disCashLog);
            disCashLog.setCashSource(request.getCashSource());
            disCashLog.setCashAmount(request.getCashAmount());
            disCashLog.setCashType(request.getCashType());
            disCashLog.setPayType(request.getPayType());
            disCashLog.setCashNo(IDUtils.GenerateBillNo("A"));
            disCashLog.setMemberId(member.getId());
            disCashLog.setStatus(1);
            Date currentTime = new Date();
            disCashLog.setApplyTime(currentTime);
            disCashLog.setCreator(member.getId());
            disCashLog.setCreateTime(currentTime);
            disCashLogMapper.insert(disCashLog);

            //修改用户可提现金额
            MememberUser mememberUser = new MememberUser();
            mememberUser.setId(member.getId());
            mememberUser.setUpdateTime(currentTime);
            mememberUser.setTotalCommission(BigDecimal.ZERO);
            mememberUserService.update(mememberUser);

            return RestResult.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("9999", e);
            throw new AppRuntimeException("9999", "提现保存失败");
        }
    }
}